var appUtils = require("../utils/app");
var utils = require("../utils/utils");
var time = require("../utils/time");
var defaultAppConfig = {
  interval: null, //每隔多少分钟执行一次
  lastStartTime: [], //上一次开始时间
  lastEndTime: null, //上一次结束时间
  installed: null, //是否安装
  storage: null,
  testOpenCount: null,
  actions: [],
  storage: null,
  runCount: null,
  maxRunCount: null,
};
function App(name, config) {
  this.name = name;
  utils.merge(this, config || {}, defaultAppConfig);
}
App.prototype.getNextAction = function () {
  for (var index = 0; index < this.actions.length; index++) {
    var action = this.actions[index];
    if (action.canFlow()) {
      return action;
    }
  }
};
App.prototype.open = function () {
  return appUtils.openApp(this.name);
};
App.prototype.close = function () {
  this.endTime = Date.now();
  return appUtils.close(this.name);
};
App.prototype.checkRunCount = function () {
  if (!this.maxRunCount) {
    return true;
  }
  this.runCount = this.storage.get("runCount") || 0;
  if (this.runCount >= this.maxRunCount) {
    console.log("已达到最大执行次数", this.name, "跳过");
    return false;
  }
  return true;
};
App.prototype.checkTestOpenCount = function () {
  if (!this.testOpenCount) {
    return true;
  }
  this.runCount = this.storage.get("runCount") || 0;
  var span = utils.getRandom(this.testOpenCount, this.testOpenCount + 1);
  if (this.runCount < span) {
    console.log(this.name, "runCount-", this.runCount, ",span-", span);
    this.open();
    sleep(utils.getRandom(6, 7.5, 1000));
    this.runTest();
    sleep(utils.getRandom(2, 3, 1000));
    this.close();
    this.storage.put("runCount", this.runCount + 1);
    return false;
  }
  return true;
};
App.prototype.checkInstalled = function () {
  if (this.installed === true || this.installed === false) {
    return this.installed;
  }
  var installed = appUtils.ifInstalled(this.name);
  this.installed = !!installed;
  if (!installed) {
    console.log("没安装", this.name, "跳过");
    return false;
  }
  return true;
};
App.prototype.checkInterval = function () {
  if (!this.interval) {
    return true;
  }
  if (!this.lastEndTime) {
    return true;
  }
  var now = Date.now();
  var wait = (
    this.interval +
    utils.getRandom(0, 2) -
    (now - this.lastEndTime) / 1000 / 60
  ).toFixed(2);
  if (wait > 0) {
    console.log(
      this.name,
      "checkInterval",
      "每隔-" + this.interval + "-分钟执行一次，还需等待-" + wait + "-分钟"
    );
    return false;
  }
  return true;
};
App.prototype.run = function () {
  console.log(this.name, "未定义run方法，请检查");
};
App.prototype.runTest = function () {
  console.log(this.name, "未定义runTest方法，请检查");
};
App.prototype.main = function () {
  if (!this.checkInstalled()) {
    return;
  }
  if (!this.checkRunCount()) {
    return;
  }
  if (!this.checkTestOpenCount()) {
    return;
  }
  if (!this.checkInterval()) {
    return;
  }
  this.open();
  this.lastStartTime = Date.now();
  sleep(utils.getRandom(6, 7.5, 1000));
  var runResult = this.run();
  if (!runResult) {
    sleep(utils.getRandom(2, 3, 1000));
    this.close();
  }
  this.lastEndTime = Date.now();
  this.runCount = this.storage.get("runCount") || 0;
  this.storage.put("runCount", this.runCount + 1);
};

module.exports = App;
